package com.kelaite.procurement.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TPurchaseOrder;
import com.kelaite.procurement.entity.TPurchaseOrderExecution;
import com.kelaite.procurement.entity.TSalesOrder;
import com.kelaite.procurement.entity.TSalesOrderExecution;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.entity.TVendor;
import com.kelaite.procurement.repository.PurchaseOrderExecutionRepository;
import com.kelaite.procurement.repository.SalesOrderExecutionRepository;
import com.kelaite.procurement.service.SalesOrderExecutionService;
import com.kelaite.procurement.utils.CurrentUserUtils;

@Service
@Transactional
public class SalesOrderExecutionServiceImpl implements SalesOrderExecutionService{
  
	@Autowired
	private SalesOrderExecutionRepository salesOrderExecutionRepository; 
	
	@Autowired
	private PurchaseOrderExecutionRepository purchaseOrderExecutionRepository;
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TSalesOrderExecution save(TSalesOrderExecution persistence) {
		return this.salesOrderExecutionRepository.save(persistence);
	}

	@Override
	public TSalesOrderExecution getObject(Long id) {
		return this.salesOrderExecutionRepository.findOne(id);
	}

	@Override
	public List<TSalesOrderExecution> getAll() {
		return this.salesOrderExecutionRepository.findAll();
	}

	@Override
	public void deleteByObject(TSalesOrderExecution persistence) {
		this.salesOrderExecutionRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.salesOrderExecutionRepository.delete(id);
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<TSalesOrderExecution> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TSalesOrderExecution.class).add(org.hibernate.criterion.Expression.eq(property, value));
		return criteria.list();
	}

	@Override
	public int confirmSalesOrderExecution(String[] orderExecutionIds) {
		int i=0;
		String cUser = CurrentUserUtils.getInstance().getUsername();

		for(String tempStr : orderExecutionIds)
		{
			if(!"".equals(tempStr))
			{	
				Long tempId=Long.parseLong(tempStr);
				this.salesOrderExecutionRepository.updateSalesOrderExecution(tempId, new Date(), new Date(), cUser);
			}
			++i;
		}
		return i;
	}

	@Transactional
	@Override
	public int uploadSalesOrderExecutionFile(String doc1, long salesOrderID,TUser currentUser,TVendor vendor) {
		
		TSalesOrder salesOrder=new TSalesOrder();
		salesOrder.setId(salesOrderID);
		
		TSalesOrderExecution orderExecution=new TSalesOrderExecution();
		orderExecution.setDelete(false);
		orderExecution.setOrderNo(1);
		orderExecution.setOrderExecutionType(false);
		orderExecution.setConfirmTime(new Date());
		orderExecution.setCreatedBy(currentUser.getUsername());
		orderExecution.setUpdatedBy(currentUser.getUsername());
		orderExecution.setDoc1(doc1);
		orderExecution.settVendor(vendor);
		orderExecution.settSalesOrder(salesOrder);
		orderExecution.settUser(currentUser);
		orderExecution.setGmtCreateTime(new Date());
		orderExecution.setGmtUpdateTime(new Date());
		salesOrderExecutionRepository.save(orderExecution);
		
		TPurchaseOrder purchaseOrder=new TPurchaseOrder();
		purchaseOrder.setId(salesOrderID);
		
		TPurchaseOrderExecution purchaseExecution=new TPurchaseOrderExecution();
		purchaseExecution.setDelete(false);
		purchaseExecution.setOrderNo(1);
		purchaseExecution.setOrderExecutionType(false);
		purchaseExecution.setConfirmTime(new Date());
		purchaseExecution.setCreatedBy(currentUser.getUsername());
		purchaseExecution.setUpdatedBy(currentUser.getUsername());
		purchaseExecution.setDoc1(doc1);
		purchaseExecution.settVendor(vendor);
		purchaseExecution.settPurchaseOrder(purchaseOrder);
		purchaseExecution.settUser(currentUser);
		purchaseExecution.setGmtCreateTime(new Date());
		purchaseExecution.setGmtUpdateTime(new Date());
		purchaseOrderExecutionRepository.save(purchaseExecution);
		
		return 0;
	}
	
}
